using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Marlimer.Common;
using Marlimer.Common.Entidades;   

namespace Marlimer.Data.Comercial
{
    public class com_DocumentoProvCab
    {
        public resulOMsgError guardarDocumento(InfoDocumentoProvCab doc)
        {
            resulOMsgError resul = new resulOMsgError(false, "", null);
            SqlCommand sqlcmd = new SqlCommand();
            Int64 cnewcod_doc = new Int64();

            //creamos la transaccion
            SqlTransaction transac = Session.getConexion().BeginTransaction("savedocprov");

            try
            {
                sqlcmd.CommandText = "spiaDocumentoProvCab";
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Connection = Session.getConexion();
                sqlcmd.Transaction = transac;

                sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);
                sqlcmd.Parameters.Add("@ctip_docvta", SqlDbType.Int);
                sqlcmd.Parameters.Add("@cnum_serie", SqlDbType.Int);
                sqlcmd.Parameters.Add("@cnum_docvta", SqlDbType.BigInt);
                sqlcmd.Parameters.Add("@dfec_ent", SqlDbType.DateTime);
                sqlcmd.Parameters.Add("@ccod_prov", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ccod_mone", SqlDbType.Int);
                sqlcmd.Parameters.Add("@nval_mone", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@ccod_condvta", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ntot_bruto", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@ntot_noafec", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@ntot_dcto", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@ntot_dctonoafec", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@npor_igv", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@cobservacion", SqlDbType.NVarChar, 50);
                sqlcmd.Parameters.Add("@cpc_crea", SqlDbType.NVarChar, 50);
                sqlcmd.Parameters.Add("@cuser_crea", SqlDbType.Int);
                sqlcmd.Parameters.Add("@cmodo", SqlDbType.Int);

                sqlcmd.Parameters["@ccod_doc"].Value = doc.ccod_doc;
                sqlcmd.Parameters["@ctip_docvta"].Value = doc.ctip_docvta;
                sqlcmd.Parameters["@cnum_serie"].Value = doc.cnum_serie;
                sqlcmd.Parameters["@cnum_docvta"].Value = doc.cnum_docvta;
                sqlcmd.Parameters["@dfec_ent"].Value = doc.dfec_ent;
                sqlcmd.Parameters["@ccod_prov"].Value = doc.ccod_prov;
                sqlcmd.Parameters["@ccod_mone"].Value = doc.ccod_mone;
                sqlcmd.Parameters["@ccod_condvta"].Value = doc.ccod_condvta;
                sqlcmd.Parameters["@nval_mone"].Value = doc.nval_mone;
                sqlcmd.Parameters["@ntot_bruto"].Value = doc.ntot_bruto;
                sqlcmd.Parameters["@ntot_noafec"].Value = doc.ntot_noafec;
                sqlcmd.Parameters["@ntot_dcto"].Value = doc.ntot_dcto;
                sqlcmd.Parameters["@ntot_dctonoafec"].Value = doc.ntot_dctonoafec;
                sqlcmd.Parameters["@npor_igv"].Value = doc.npor_igv;
                sqlcmd.Parameters["@cobservacion"].Value = doc.cobservacion;
                sqlcmd.Parameters["@cpc_crea"].Value = doc.cpc_crea;
                sqlcmd.Parameters["@cuser_crea"].Value = doc.cuser_crea;
                sqlcmd.Parameters["@cmodo"].Value = doc.cmodo;

                long ccod_doc = 0;
                if (doc.cmodo == 1)
                {//insertar
                    ccod_doc = Convert.ToInt64(sqlcmd.ExecuteScalar());
                }
                else
                {//actualizar
                    ccod_doc = doc.ccod_doc;
                    sqlcmd.ExecuteNonQuery();
                }

                sqlcmd.CommandText = "spiDocumentoProvDet";
                sqlcmd.Transaction = transac;
                sqlcmd.Parameters.Clear();

                sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);
                sqlcmd.Parameters.Add("@ccod_prod", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ncant", SqlDbType.Int);
                sqlcmd.Parameters.Add("@ccod_unimed", SqlDbType.Int);
                sqlcmd.Parameters.Add("@npre_comp", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@npor_dcto", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@flg_noafecigv", SqlDbType.Decimal);
                sqlcmd.Parameters.Add("@npor_igv", SqlDbType.Decimal);

                foreach (InfoDocumentoProvDet docdet in doc.documentoDetProv)
                {
                    sqlcmd.Parameters["@ccod_doc"].Value = ccod_doc;
                    sqlcmd.Parameters["@ccod_prod"].Value = docdet.ccod_prod;
                    sqlcmd.Parameters["@ncant"].Value = docdet.ncant;
                    sqlcmd.Parameters["@ccod_unimed"].Value = docdet.ccod_unimed;
                    sqlcmd.Parameters["@npre_comp"].Value = docdet.npre_comp;
                    sqlcmd.Parameters["@npor_dcto"].Value = docdet.npor_dcto;
                    sqlcmd.Parameters["@flg_noafecigv"].Value = docdet.flg_noafecigv;
                    sqlcmd.Parameters["@npor_igv"].Value = docdet.npor_igv;
                    sqlcmd.ExecuteNonQuery();
                }
                //terminamos la transaccion
                transac.Commit();

                resul.message = "Se guardo el documento satisfactoriamente.";
                cnewcod_doc = (Int64)ccod_doc;
                resul.clase = cnewcod_doc;
                resul.resul = true;
            }
            catch (Exception ex)
            {
                transac.Rollback();
                resul.message = ex.Message; 
            }
            return resul;
        }

        public DataTable DocumentoCabByCod(long ccod_doc)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsDocumentoProvCabByCod";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);

            sqlcmd.Parameters["@ccod_doc"].Value = ccod_doc;

            da.SelectCommand = sqlcmd;
            da.Fill(ds); 

            return ds.Tables[0]; 
        }

        public int existeDocumento(int ctip_docvta, int cnum_serie, long cnum_docvta, int ccod_prov)
        {
            SqlCommand sqlcmd = new SqlCommand();
            

            sqlcmd.CommandText = "spsExisteDocumentoProv";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ctip_docvta", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cnum_serie", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cnum_docvta", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@ccod_prov", SqlDbType.Int);

            sqlcmd.Parameters["@ctip_docvta"].Value = ctip_docvta;
            sqlcmd.Parameters["@cnum_serie"].Value = cnum_serie;
            sqlcmd.Parameters["@cnum_docvta"].Value = cnum_docvta;
            sqlcmd.Parameters["@ccod_prov"].Value = ccod_prov;

            int resul = Convert.ToInt32(sqlcmd.ExecuteScalar());

            return resul;             
        }

        public resulMsgError anularDocumento(long ccod_doc)
        {
            resulMsgError resul = new resulMsgError(false, "");
            SqlCommand sqlcmd = new SqlCommand();
 
            try
            {
                sqlcmd.CommandText = "spuAnularDocumentoProv";
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Connection = Session.getConexion();

                sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);
                sqlcmd.Parameters.Add("@cpc_crea", SqlDbType.NVarChar, 50);
                sqlcmd.Parameters.Add("@cuser_crea", SqlDbType.Int);

                sqlcmd.Parameters["@ccod_doc"].Value = ccod_doc;
                sqlcmd.Parameters["@cpc_crea"].Value = Session.getComputerName();
                sqlcmd.Parameters["@cuser_crea"].Value = Session.getInfousuario().ccod_user;

                sqlcmd.ExecuteNonQuery();

                resul.resul = true;
                resul.message = "Se anulo el documento de compra satisfactoriamente";
            }
            catch (Exception ex)
            {
                resul.message = ex.Message; 
            }

            return resul; 
        }

        public DataTable DocumentoDetByCod(long ccod_doc)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();

            sqlcmd.CommandText = "spsDocumentoProvDetByCod";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();

            sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);
            sqlcmd.Parameters["@ccod_doc"].Value = ccod_doc;

            da.SelectCommand = sqlcmd;
            da.Fill(ds);

            return ds.Tables[0]; 
        }

        public DataTable consultaDocumento(int ctip_docvta, DateTime dfec_ini, DateTime dfec_fin, int cnum_serie, long cnum_desde, long cnum_hasta, int ccod_prov, int ctip_bus, int flg_anulados)
        {
            SqlCommand sqlcmd = new SqlCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            DataSet ds = new DataSet();
 
            sqlcmd.CommandText = "spsConsultarDocumentosProv";
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Connection = Session.getConexion();
             
            sqlcmd.Parameters.Add("@ctip_docvta", SqlDbType.Int);  
            sqlcmd.Parameters.Add("@dfec_ini", SqlDbType.DateTime);
            sqlcmd.Parameters.Add("@dfec_fin", SqlDbType.DateTime);
            sqlcmd.Parameters.Add("@cnum_serie", SqlDbType.Int);
            sqlcmd.Parameters.Add("@cnum_desde", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@cnum_hasta", SqlDbType.BigInt);
            sqlcmd.Parameters.Add("@ccod_prov", SqlDbType.Int);
            sqlcmd.Parameters.Add("@flg_anulados", SqlDbType.Int);
            sqlcmd.Parameters.Add("@ctip_bus", SqlDbType.Int);
            

            sqlcmd.Parameters["@ctip_docvta"].Value = ctip_docvta;
            sqlcmd.Parameters["@dfec_ini"].Value = dfec_ini;
            sqlcmd.Parameters["@dfec_fin"].Value = dfec_fin;
            sqlcmd.Parameters["@cnum_serie"].Value = cnum_serie;
            sqlcmd.Parameters["@cnum_desde"].Value = cnum_desde;
            sqlcmd.Parameters["@cnum_hasta"].Value = cnum_hasta;
            sqlcmd.Parameters["@ccod_prov"].Value = ccod_prov;
            sqlcmd.Parameters["@flg_anulados"].Value = flg_anulados;
            sqlcmd.Parameters["@ctip_bus"].Value = ctip_bus;
           
            da.SelectCommand = sqlcmd;
            da.Fill (ds);

            return ds.Tables[0];

        }

        public resulMsgError activarDocumentoProv(long ccod_doc)
        {
            resulMsgError resul = new resulMsgError(false, "");
            SqlCommand sqlcmd = new SqlCommand();

            try
            {
                sqlcmd.CommandText = "spuActivarDocumentoProv";
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Connection = Session.getConexion();

                sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);
                sqlcmd.Parameters.Add("@cpc_crea", SqlDbType.NVarChar, 50);
                sqlcmd.Parameters.Add("@cuser_crea", SqlDbType.Int);

                sqlcmd.Parameters["@ccod_doc"].Value = ccod_doc;
                sqlcmd.Parameters["@cpc_crea"].Value = Session.getComputerName();
                sqlcmd.Parameters["@cuser_crea"].Value = Session.getInfousuario().ccod_user;

                sqlcmd.ExecuteNonQuery();

                resul.resul = true;
                resul.message = "Se activo el documento de compra satisfactoriamente";
            }
            catch (Exception ex)
            {
                resul.message = ex.Message;
            }

            return resul;
        }

        public resulMsgError pagarDocumento(List<InfoDocumentoProvCab> infosDocumentoProvCab)
        {
            resulMsgError resul = new resulMsgError(false, "");
            SqlCommand sqlcmd = new SqlCommand();

            try
            {
                sqlcmd.CommandText = "spuPagarDocumentoProv";
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Connection = Session.getConexion();

                sqlcmd.Parameters.Add("@ccod_doc", SqlDbType.BigInt);
                sqlcmd.Parameters.Add("@cpc_crea", SqlDbType.NVarChar, 50);
                sqlcmd.Parameters.Add("@cuser_crea", SqlDbType.Int);

                string computer = Session.getComputerName();
                int user = Session.getInfousuario().ccod_user;

                foreach (InfoDocumentoProvCab infoDocumentoProvCab in infosDocumentoProvCab)
                {
                    sqlcmd.Parameters["@ccod_doc"].Value = infoDocumentoProvCab.ccod_doc;
                    sqlcmd.Parameters["@cpc_crea"].Value = computer;
                    sqlcmd.Parameters["@cuser_crea"].Value = user;
                    sqlcmd.ExecuteNonQuery();
                }
                
                

                resul.resul = true;
                resul.message = "Se activo el documento de compra satisfactoriamente";
            }
            catch (Exception ex)
            {
                resul.message = ex.Message;
            }

            return resul;
        }
    }
}
